package com.nwpu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nwpu.domain.Goods;
import com.nwpu.domain.SecKillGoods;
import com.nwpu.mapper.GoodsMapper;
import com.nwpu.service.GoodsService;
import com.nwpu.util.DateUtil;
import com.nwpu.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* @author DPH
* @description 针对表【tbl_goods】的数据库操作Service实现
* @createDate 2023-05-23 11:16:05
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private DateUtil dateUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public boolean saveGoods(Goods goods) {
        goods.setCreateTime(new Date());
        goods.setUpdateTime(new Date());
        return goodsMapper.insert(goods) > 0;
    }

    @Override
    public Goods getGoodsById(String id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public boolean updateGoods(Goods goods) {
        goods.setUpdateTime(new Date());
        return goodsMapper.updateById(goods) > 0;
    }

    @Override
    public boolean deleteGoodsById(String id) {
        return goodsMapper.deleteById(id) > 0;
    }

    @Override
    @Scheduled(cron = "0 0 1 * * ?")
    public void addSecKillGoods() {
        LambdaQueryWrapper<Goods> lq = Wrappers.lambdaQuery();
        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
        lq.between(Goods::getBeginTime, start, end).select(Goods::getGoodsId, Goods::getBeginTime, Goods::getEndTime, Goods::getInventory);
        List<Goods> list = goodsMapper.selectList(lq);
        for (Goods goods : list) {
            long endTime = goods.getEndTime().getTime();
            long currentTime = System.currentTimeMillis();
            //long beginTime = goods.getBeginTime().getTime();
            //long secKillTime=(endTime - beginTime)/1000/60;
            String key = "GOODS_" + goods.getGoodsId();
            SecKillGoods secKillGoods = new SecKillGoods();
            secKillGoods.setGoodsName(goods.getGoodsName());
            secKillGoods.setInventory(goods.getInventory());
            secKillGoods.setBeginTime(goods.getBeginTime());
            secKillGoods.setEndTime(goods.getEndTime());
            redisUtil.set(key, secKillGoods, endTime - currentTime);
            String key1 = "GOODS_INVENTORY_" + goods.getGoodsId();
            redisUtil.set(key1, goods.getInventory(), endTime - currentTime);
        }
    }
}




